package cn.tedu.sp07.controller;

import cn.tedu.spo1.pojo.Item;
import cn.tedu.spo1.pojo.Order;
import cn.tedu.spo1.pojo.User;
import cn.tedu.web.util.JsonResult;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class RibbonController {
    @Autowired
    private RestTemplate rt;

    //调用远程商品服务-如果跳转失败跳转到另一个方法中
    @HystrixCommand(fallbackMethod = "getItemsFB")
    @GetMapping("/item-service/{orderId}")
    public JsonResult<List<Item>> getItems(
            @PathVariable String orderId){

        //{1}是RestTemplate定义的一种占位符格式，后面第三个参数orderId会对占位符进行填充
        return rt.getForObject("http://item-service/{1}", JsonResult.class,orderId);
    }

    @HystrixCommand(fallbackMethod = "decreaseNumberFB")
    @PostMapping("/item-service/decreaseNumber")
    public JsonResult<?> decreaseNumber(@RequestBody List<Item> items) {
        //调用商品服务，减少商品库存
        return rt.postForObject("http://item-service/decreaseNumber",
                items, //post请求的数据
                JsonResult.class);
    }

    @HystrixCommand(fallbackMethod = "getUserFB")
    @GetMapping("/user-service/{userId}")
    public JsonResult<User> getUser(
            @PathVariable String userId){

        //{1}是RestTemplate定义的一种占位符格式，后面第三个参数orderId会对占位符进行填充
        return rt.getForObject("http://user-service/{1}", JsonResult.class,userId);
    }

    @HystrixCommand(fallbackMethod = "addScoreFB")
    @GetMapping("/user-service/{userId}/score")
    public JsonResult<?>addScore(
            @PathVariable Integer userId,Integer score){

        return rt.getForObject("http://user-service/{1}/score?score={2}",
                JsonResult.class,
                userId,score);

    }

    @HystrixCommand(fallbackMethod = "getOrderFB")
    @GetMapping("/order-service/{orderId}")
    public JsonResult<Order> getOrder(
            @PathVariable String orderId) {

        return rt.getForObject("http://order-service/{1}",
                JsonResult.class,
                orderId);
    }

    @HystrixCommand(fallbackMethod = "AddOrderFB")
    @GetMapping("/order-service")
    public JsonResult addOrder() {

        return rt.getForObject("http://order-service/",
                JsonResult.class);
    }
    /////////////////////////////////////////////////
    public JsonResult<List<Item>> getItemsFB(String orderId){
        return  JsonResult.err().msg("获取订单的商品列表失败1");

    }

    public JsonResult<?> decreaseNumberFB(List<Item> items) {
        return  JsonResult.err().msg("减少商品库存失败");
    }

    public JsonResult<User> getUserFB(String userId){
        return  JsonResult.err().msg("获取用户失败");
    }

    public JsonResult<?>addScoreFB(Integer userId,Integer score){
        return  JsonResult.err().msg("增加用户积分失败");

    }

    public JsonResult<Order> getOrderFB(String orderId) {
        return  JsonResult.err().msg("获取订单失败");
    }

    public JsonResult addOrderFB() {
        return  JsonResult.err().msg("添加订单失败");
    }


}
